package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    @Override
    public List<NursingPlanVo> findAll() {

        List<NursingPlan> nursingPlans =nursingPlanMapper.findAll();
        List<NursingPlanVo> nursingPlanVos = new ArrayList<>();
        for (NursingPlan nursingPlan : nursingPlans) {
            BeanUtils.copyProperties(nursingPlan,nursingPlanVos);
        }
        return nursingPlanVos;

    }


    //分页查询
    @Override
    public PageResponse<NursingPlanVo> page(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.selectPage(name, status);
        PageResponse<NursingPlanVo> pageResponse = PageResponse.of(nursingPlans, NursingPlanVo.class);
        return pageResponse;
    }

    @Override
    public NursingPlanVo findById(Long id) {
        return nursingPlanMapper.findById(id);

    }

    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        //添加plan计划将传过来的Dto转为实体类
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.add(nursingPlan);
        //获取plan实体类中的projectPlans集合（这是中间表projectplan表中的数据）
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        //批量给projectplanDto赋值planid
        projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
        //传给中间表的service层，数据可能是多个所以要用数组形式
        nursingProjectPlanService.adds(projectPlans);//已经存了planid的中间表的DTO


    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        //根据id更新plan表
        nursingPlanMapper.update(BeanUtil.toBean(nursingPlanDto, NursingPlan.class));
        //根据planid更改project_plan表，可能是多个所有要用集合接收
        nursingProjectPlanService.update(nursingPlanDto.getProjectPlans());
    }


    /**
     * 删除plan表以及中间表project_plan
     * 中间表根据planid删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        nursingPlanMapper.deleteById(id);
        nursingProjectPlanService.deletcByPlanId(id);

    }

    /**
     * 根据id修改status
     * @param id
     * @param status
     */
    @Override
    public void status(Long id, Integer status) {
        nursingPlanMapper.status(id,status);
    }
}
